package haida.pym_system.site.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import haida.pym_system.site.pojo.Site;
import haida.pym_system.site.pojo.User;
import haida.pym_system.site.pojo.Vo;
import haida.pym_system.site.service.impl.SiteServiceImpl;
import haida.pym_system.utils.Time;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 大龙
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/site")
public class SiteController {
    @Autowired
    private SiteServiceImpl siteService;
    @Autowired
    Time time;

    /**
     * 添加场地
     */
    @RequestMapping("/addSite")

    public ResponseEntity<Object> addSite(@RequestBody Site addSiteForm) {
        addSiteForm.setCreateTime(time.getCurrentTime());
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute("loginUser");
        System.out.println(user.getUserID());
        addSiteForm.setCreater(user.getUserID());
        System.out.println(addSiteForm);
        siteService.addSite(addSiteForm);
        return new ResponseEntity<>(addSiteForm, HttpStatus.OK);

    }




    /**
     * 删除场地
     *
     * @param siteID
     */
    @RequestMapping("/deleteSite")
    @ResponseBody
    public ResponseEntity<Object> deleteSite(int siteID) {
        siteService.deleteSite(siteID);
        return new ResponseEntity<>(200, HttpStatus.OK);

    }

    @RequestMapping("/toUpdateSite/{siteID}")
    public ResponseEntity<Object> toUpdateSite(@PathVariable int siteID) {
        Site site = siteService.findASite(siteID);
        return new ResponseEntity<>(200, HttpStatus.OK);
    }

    /**
     * 修改场地信息
     *
     * @param site
     * @return
     */
    @RequestMapping("/updateSite")
    public ResponseEntity<Object> updateSite(Site site) {
        System.out.println(site);
        site.setCreateTime(time.getCurrentTime());
        siteService.updateSite(site);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 查询全部场地
     *
     * @return
     */
    @RequestMapping("/findAllSite")
    public ResponseEntity<Object> findAllSite(String content,int current,int size) {
        Vo<Site> allSite = siteService.findAllSite(content, current, size);
        return new ResponseEntity<>(allSite, HttpStatus.OK);
    }

    /**
     * 查询一个场地
     *
     * @param siteID
     * @return
     */
    @RequestMapping("/findASite/{siteID}")
    public ResponseEntity<Object> findASite(@PathVariable int siteID) {
        Site site = siteService.findASite(siteID);
        return new ResponseEntity<>(site,HttpStatus.OK);

    }

    /**
     * 查询所有的可用的场地
     *
     * @return
     */
    @RequestMapping("/findAllAbleSite")
    @ResponseBody
    public ResponseEntity<Object> findAllAbleSite() {
        List<Site> ableSite = siteService.findAllAbleSite();
        return new ResponseEntity<>(ableSite, HttpStatus.OK);
    }

}


